package com.example.myapplication.tack_14;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_14.adapter.ClassificaAdapter;
import com.example.myapplication.tack_14.data.BannerData;
import com.example.myapplication.tack_14.data.ClassificationData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner;

    private RecyclerView fenlei;
    private SearchView search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_14_activity_search);
        init();
        exit_();
        getBanner();
        getFenLei();
        search_();
    }

    private void search_() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                submit(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Change(newText);
                return true;
            }
        });
    }

    private void Change(String newText) {
        if("".equals(newText) || TextUtils.isEmpty(newText)){
            Toast.makeText(SearchActivity.this,"请输入值",Toast.LENGTH_SHORT).show();
            getFenLei();
        }else{
            getSearch(newText);
        }
    }

    private void submit(String query) {
        if("".equals(query) || TextUtils.isEmpty(query)){
            getFenLei();
        }else{
            getSearch(query);
        }
    }
    //搜索
    private void getSearch(String query) {{
        String url = "prod-api/api/garbage-classification/garbage-example/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ClassificationData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ClassificationData classificationData = (ClassificationData)obj;
                if(classificationData.getCode() == 200){
                    List<ClassificationData.RowsDTO> classList = classificationData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (ClassificationData.RowsDTO row : classificationData.getRows()) {
                                if(query.toLowerCase().toLowerCase().contains(row.getName())){
                                    classList.add(row);
                                }
                            }
                            FenleiAdater(classList);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }}
    private void getFenLei() {
        String url = "prod-api/api/garbage-classification/garbage-example/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ClassificationData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ClassificationData classificationData = (ClassificationData)obj;
                if(classificationData.getCode() == 200){
                    List<ClassificationData.RowsDTO> classList = classificationData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FenleiAdater(classList);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void FenleiAdater(List<ClassificationData.RowsDTO> classList) {
        ClassificaAdapter classificaAdapter = new ClassificaAdapter(classList, SearchActivity.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchActivity.this,4);
        fenlei.setLayoutManager(gridLayoutManager);
        fenlei.setAdapter(classificaAdapter);
    }

    private void getBanner() {
        String url = "prod-api/api/garbage-classification/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> arrayList = new ArrayList<>();
                            for (BannerData.DataDTO row : bannerData.getData()) {
                                arrayList.add(row.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(SearchActivity.this,arrayList,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        fenlei = findViewById(R.id.fenlei);
        search = findViewById(R.id.search);
    }
}